<?
	$f = $s = $t = $fth = '';
	$f_l = 'N/A';
	$s_l = 'N/A';
	$t_l = 'N/A';
	$fth_l = 'N/A';
								
	function get_ave($f, $s, $t, $fth)
	{
		$ave = 'N/A';
		$letter = '';
		if(!empty($f) && !empty($s) && !empty($t) && !empty($fth))
		{
			$ave = $f + $s + $t + $fth;
			$ave = $ave / 4;
			$ave = round($ave, 2, PHP_ROUND_HALF_EVEN);
			
			if($ave >= 90)
			{
				$letter = 'A';
			}elseif($ave >= 85)
			{
				$letter = 'P';
			}elseif($ave >= 80)
			{
				$letter = 'AP';
			}elseif($ave >= 75)
			{
				$letter = 'D';
			}else
			{
				$letter = 'B';
			}
		}
		
		return $ave . ' (' . $letter . ')';
	}
	
	function get_remark($value)
	{
		$remark = '';
		if($value == 'F')
		{
			$remark = 'Failed';
		}else
		{
			$remark = 'Passed';
		}
		return $remark;
	}
	
	function _print_period($index)
	{
		$cardinal = '';
		switch($index)
		{
			case 1:
				$cardinal = '1st';
				break;
			case 2:
				$cardinal = '2nd';
				break;
			case 3:
				$cardinal = '3rd';
				break;
			case 4:
				$cardinal = '4th';
				break;
			default:
				$cardinal = $index;
				break;
		}
		return $cardinal;
	}
?>

<div class="row">
	<div class="panel callout"><h6>Grades</h6></div>
		<? if(isset($system_message)) echo $system_message;?>
		<?require 'grades_nav.php';?>
		<div class="panel"><h6><?=$subj_data->subj_code . ' (' . $subj_data->subj_desc . ')';?></h6></div>
		<?if(!empty($students)): ?>
			<?if(!empty($grades_data)):?>
				<form action="<?=site_url(array('grades','subject_grades',$id));?>" method="POST" class="custom submit_forms">
					<table>
						<thead>
							<tr>
								<th rowspan="2">Student</th>
								<th colspan="<?=$periods;?>">Grading Period</th>
								<?if($periods > 3):?>
									<th rowspan="2">Average</th>
									<th rowspan="2">Remark</th>
								<?endif;?>
								
							</tr>
							<tr>
								<?for($i = 0; $i < $periods; $i++):?>
									<th><?=_print_period($i + 1);?></th>
								<?endfor;?>
							</tr>
						</thead>
						<tbody>
							<?foreach($students AS $s):?>
								<tr>
									<td><?=$s->lastname . ', ' . $s->firstname . ' ' . $s->middlename;?></td>
									<?for($i =1; $i <= $periods; $i++):?>
										<?foreach($grades_data AS $g):?>
											<?if($g->grading_period_id == $i):?>
												<?if($s->e_id == $g->enrollment_id):?>
													<td>
														<?if($g->value > 0):?>
															<p><?=$g->value;?></p>
															<p><?=$g->letter_grade?></p>
															<input type="hidden" id="<?=$s->e_id . '_' . $i;?>" value="<?=$g->value?>">
														<?else:?>
															<input type="text" value="<?=$g->value?>" class="grade_input <?if($periods == 4) echo 'fourth_gp';?>" name="grades[<?=$s->e_id?>][<?=$i?>]" id="<?=$s->e_id . '_' . $i;?>"/>
															<p id="lg_<?=$s->e_id . '_' . $i;?>">N/A</p>
															<input type="hidden" name="grades[<?=$s->e_id?>][e_id]" value="<?=$s->e_id;?>" />
														<?endif;?>
													</td>
													
												<?endif;?>
											<?endif;?>
										<?endforeach;?>
									<?endfor;?>
									<?if($periods == 4):?>
										<td><input type="text" name="grades[<?=$s->e_id;?>][ave]" id="<?=$s->e_id . '_ave';?>"/>
											<p id="<?=$s->e_id . '_disp';?>">N/A</p>
										</td>
										<td><input type="text" name="grades[<?=$s->e_id;?>][remark]" id="<?=$s->e_id . '_rem'?>"/></td>
									<?endif;?>
								</tr>
							<?endforeach;?>
						</tbody>
					</table>
					<a href="#" class="button small success submit_btns">Submit Grades</a>
				</form>
			<?else:?>
				<div data-alert class="alert-box secondary">There is no recorded grades for this subject. <a href="<?=site_url(array('grades', 'new_grades', $subj_data->subj_id))?>">Click here to create one now.</a></div>
			<?endif;?>
		<?else: ?>
			<div data-alert class="alert-box">
				Either:
				<div>
					<ul class="disc">
						<li>No students have been assigned to you, or</li>
						<li>There was an error retrieving students.</li>
					</ul>
				</div>
				Contact your administrator.
			</div>
		<?endif;?>			
</div>

